<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>生成更好的随机数</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mt-getrandmax.html">mt_getrandmax</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mt-srand.html">mt_srand</a></div>
 <div class="up"><a href="ref.math.html">Math 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.mt-rand" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mt_rand</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mt_rand</span> &mdash; <span class="dc-title">生成更好的随机数</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.mt-rand-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>mt_rand</strong></span>
    ( <span class="methodparam">void</span>
   )</div>

  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>mt_rand</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$min</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$max</code></span>
   )</div>

    <p class="simpara">
     很多老的 libc
     的随机数发生器具有一些不确定和未知的特性而且很慢。PHP
     的 <span class="function"><a href="function.rand.html" class="function">rand()</a></span> 函数默认使用
     libc 随机数发生器。<span class="function"><strong>mt_rand()</strong></span>
     函数是非正式用来替换它的。该函数用了
     <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;Mersenne Twister</a>
     中已知的特性作为随机数发生器，它可以产生随机数值的平均速度比
     libc 提供的 rand() 快四倍。
    </p>
    <p class="simpara">
     如果没有提供可选参数 <em><code class="parameter">min</code></em> 和
     <em><code class="parameter">max</code></em>，<span class="function"><strong>mt_rand()</strong></span>
     返回 0 到 <span class="function"><a href="function.mt-getrandmax.html" class="function">mt_getrandmax()</a></span>
     之间的伪随机数。例如想要 5 到 15（包括 5 和 15）之间的随机数，用
     <em>mt_rand(5, 15)</em>。
    </p>
   </div>

 <div class="refsect1 parameters" id="refsect1-function.mt-rand-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">min</code></em></dt>

     <dd>

      <p class="para">
       可选的、返回的最小值（默认：0）
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">max</code></em></dt>

     <dd>

      <p class="para">
       可选的、返回的最大值（默认：<span class="function"><a href="function.mt-getrandmax.html" class="function">mt_getrandmax()</a></span>）
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.mt-rand-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 <em><code class="parameter">min</code></em> （或者 0） 到 <em><code class="parameter">max</code></em> （或者是到  <span class="function"><a href="function.mt-getrandmax.html" class="function">mt_getrandmax()</a></span>
     ，包含这个值）之间的随机整数。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.mt-rand-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr><td>4.2.0</td><td>随机数发生器自动进行播种。</td></tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.mt-rand-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3375">
    <p><strong>Example #1 <span class="function"><strong>mt_rand()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">15</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
1604716014
1478613278
6
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.mt-rand-notes">
  <h3 class="title">注释</h3>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    The distribution of <span class="function"><strong>mt_rand()</strong></span> return values is biased
    towards even numbers on 64-bit builds of PHP when
    <em><code class="parameter">max</code></em> is beyond <em>2^32</em>.
   </p>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.mt-rand-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mt-srand.html" class="function" rel="rdfs-seeAlso">mt_srand()</a> - 播下一个更好的随机数发生器种子</span></li>
    <li class="member"><span class="function"><a href="function.mt-getrandmax.html" class="function" rel="rdfs-seeAlso">mt_getrandmax()</a> - 显示随机数的最大可能值</span></li>
    <li class="member"><span class="function"><a href="function.rand.html" class="function" rel="rdfs-seeAlso">rand()</a> - 产生一个随机整数</span></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mt-getrandmax.html">mt_getrandmax</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mt-srand.html">mt_srand</a></div>
 <div class="up"><a href="ref.math.html">Math 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
